package com.ruoyi.points.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;

import com.ruoyi.ssh.domain.Course;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.points.domain.OrderInfo;
import com.ruoyi.points.service.IOrderInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单管理Controller
 * 
 * @author jion
 * @date 2025-08-13
 */
@RestController
@RequestMapping("/points/info")
public class OrderInfoController extends BaseController
{
    @Autowired
    private IOrderInfoService orderInfoService;

    // 权限控制
    @PreAuthorize("@ss.hasPermi('points:info:list')")
    @GetMapping("/userWith")
    public TableDataInfo listWithRelations(OrderInfo orderInfo) {

        startPage();
        List<OrderInfo> list = orderInfoService.selectOrderInfoAndUser(orderInfo);

        return getDataTable(list);
    }
    /**
     * 查询订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('points:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderInfo orderInfo)
    {
        startPage();
        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        return getDataTable(list);
    }

    @PreAuthorize("isAuthenticated()")
    @GetMapping("/userList")
    public TableDataInfo userList(OrderInfo orderInfo)
    {
        // 在方法内部进行权限验证
        if (orderInfo.getUserId() != null) {
            // 如果指定了userId，验证是否是当前用户
            if (!SecurityUtils.getUserId().equals(orderInfo.getUserId())) {
                throw new RuntimeException("您只能查看自己的订单");
            }
        } else {
            // 如果没有指定userId，默认查询当前用户的订单
            orderInfo.setUserId(SecurityUtils.getUserId().intValue());
        }

        startPage();
        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        return getDataTable(list);
    }
    /**
     * 导出订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('points:info:export')")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderInfo orderInfo)
    {
        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        ExcelUtil<OrderInfo> util = new ExcelUtil<OrderInfo>(OrderInfo.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('points:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(orderInfoService.selectOrderInfoById(id));
    }

    /**
     * 新增订单管理
     */
    @PreAuthorize("@ss.hasPermi('points:info:add')")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderInfo orderInfo)
    {
        return toAjax(orderInfoService.insertOrderInfo(orderInfo));
    }

    /**
     * 修改订单管理
     */
    @PreAuthorize("@ss.hasPermi('points:info:edit')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderInfo orderInfo)
    {
        return toAjax(orderInfoService.updateOrderInfo(orderInfo));
    }

    /**
     * 删除订单管理
     */
    @PreAuthorize("@ss.hasPermi('points:info:remove')")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(orderInfoService.deleteOrderInfoByIds(ids));
    }

}
